# 物品相关的API路由
from fastapi import APIRouter, HTTPException
from typing import List
from app.schemas import Item
from app import database

router = APIRouter(prefix="/items", tags=["物品管理"])

@router.get("", response_model=List[Item], summary="获取所有物品")
async def get_items():
    """获取所有物品列表"""
    return database.get_all_items()

@router.get("/{item_id}", response_model=Item, summary="根据ID获取物品")
async def get_item(item_id: int):
    """根据物品ID获取详细信息"""
    item = database.get_item_by_id(item_id)
    if not item:
        raise HTTPException(status_code=404, detail="物品不存在")
    return item

@router.post("", response_model=Item, status_code=201, summary="创建新物品")
async def create_item(item: Item):
    """创建新的物品"""
    return database.create_item(item)

@router.put("/{item_id}", response_model=Item, summary="更新物品")
async def update_item(item_id: int, item: Item):
    """更新指定ID的物品信息"""
    updated_item = database.update_item(item_id, item)
    if not updated_item:
        raise HTTPException(status_code=404, detail="物品不存在")
    return updated_item

@router.delete("/{item_id}", summary="删除物品")
async def delete_item(item_id: int):
    """删除指定ID的物品"""
    deleted_item = database.delete_item(item_id)
    if not deleted_item:
        raise HTTPException(status_code=404, detail="物品不存在")
    return {"message": "删除成功", "item": deleted_item}

